package com.jin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jin.bean.TruckUnit;
import com.jin.common.ResponseResult;
import com.jin.service.TruckUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created on 2025/5/14.
 * @author Zhongyuan Jin
 */
@RestController
@RequestMapping("/truck-unit")
public class TruckUnitController {

    @Autowired
    private TruckUnitService truckUnitService;

    //获取全部车辆信息
    @GetMapping("/all")
    public ResponseResult<List<TruckUnit>> getAllTruckUnits() {
        List<TruckUnit> allTruckUnits = truckUnitService.list();
        return ResponseResult.success("获取全部车辆信息", allTruckUnits);

    }

    // 获取所有车辆信息（带分页）
    @GetMapping("/list")
    public ResponseResult<List<TruckUnit>> list(@RequestParam(defaultValue = "1") int pageNum,
                                                @RequestParam(defaultValue = "10") int pageSize) {
        Page<TruckUnit> page = new Page<>(pageNum, pageSize);
        Page<TruckUnit> truckUnitPage = truckUnitService.page(page);
        List<TruckUnit> records = truckUnitPage.getRecords();
        long total = truckUnitPage.getTotal();
        return ResponseResult.success("获取所有车辆信息（带分页）", records, total);

    }

    // 根据ID获取车辆信息
    @GetMapping("/{id}")
    public ResponseResult<TruckUnit> getById(@PathVariable Integer id) {
        TruckUnit truckUnit = truckUnitService.getById(id);
        if (truckUnit == null) {
            return ResponseResult.fail("未找到该车辆信息", null);
        }
        return ResponseResult.success("根据ID获取车辆信息", truckUnit);

    }

    // 新增车辆信息
    @PostMapping
    public ResponseResult<Boolean> add(@RequestBody TruckUnit truckUnit) {
        boolean result = truckUnitService.save(truckUnit);
        return new ResponseResult<>(result ? 0 : 1, result ? "添加成功" : "添加失败", result, null);
    }

    // 更新车辆信息
    @PutMapping
    public ResponseResult<Boolean> update( @RequestBody TruckUnit truckUnit) {

        boolean result = truckUnitService.updateById(truckUnit);
        return new ResponseResult<>(result ? 0 : 1, result ? "更新成功" : "更新失败", result,  null);
    }

    // 删除车辆信息
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = truckUnitService.removeById(id);
        return new ResponseResult<>(result ? 0 : 1, result ? "删除成功" : "删除失败", result,  null);
    }

    // 启用车辆信息
    @PutMapping("/enable/{id}")
    public ResponseResult<Boolean> enable(@PathVariable Integer id) {
        TruckUnit truckUnit = truckUnitService.getById(id);
        if (truckUnit == null) {
            return ResponseResult.fail("未找到该车辆信息", false);

        }
        truckUnit.setIsEnable(1); // 启用
        boolean result = truckUnitService.updateById(truckUnit);
        return new ResponseResult<>(result ? 0 : 1, result ? "启用成功" : "启用失败", result,  null);
    }

    // 禁用车辆信息
    @PutMapping("/disable/{id}")
    public ResponseResult<Boolean> disable(@PathVariable Integer id) {
        TruckUnit truckUnit = truckUnitService.getById(id);
        if (truckUnit == null) {
            return ResponseResult.fail("未找到该车辆信息", false);
        }
        truckUnit.setIsEnable(0); // 禁用
        boolean result = truckUnitService.updateById(truckUnit);
        return new ResponseResult<>(result ? 0 : 1, result ? "禁用成功" : "禁用失败", result,  null);
    }

    // 按车牌号模糊查询
    @GetMapping("/search")
    public ResponseResult<List<TruckUnit>> searchByPlate(@RequestParam String plate) {
        List<TruckUnit> truckUnits = truckUnitService.list(new QueryWrapper<TruckUnit>().like("plate", plate));
        return ResponseResult.success("按车牌号模糊查询成功", truckUnits);
    }
}